﻿var lang = getPageLang();

$(document).ready(function () {
    if ($("#StampPoll2010").length > 0) {
        validate("INDEX");
    }
    if ($("#PollForm").length > 0) {
        var fboptions = { "transitionIn": "elastic", "transitionOut": "elastic" };

        validate("POLL");

        $("#PollForm").validate();
        $("#PollForm #SouvenirError").text(getMsg("SOUVENIRQUIRED", lang)).hide();
        $("#PollForm #StampError").text(getMsg("STAMPQUIRED", lang)).hide();
        //$("#PollForm .souvenirbox").rules("add", { required: true, maxlength: 3 });
        //$("#PollForm .stampbox").rules("add", { required: true, maxlength: 3 });
        $("#PollForm #Name").rules("add", { required: true, messages: { required: getMsg("NAMEQUIRED", lang)} });
        $("#PollForm #Id").rules("add", { required: true, digits: true, minlength: 5, messages: { required: getMsg("IDNUMQUIRED", lang), digits: getMsg("NUMQUIRED", lang), minlength: getMsg("NUMQUIRED", lang)} });
        //$("#PollForm #Email").rules("add", { required: true, email: true, messages: { required: getMsg("EMAILQUIRED", lang), email: getMsg("EMAILVALID", lang)} });
        $("#PollForm #Tel").rules("add", { required: true, digits: true, minlength: 8, messages: { required: getMsg("TELQUIRED", lang), digits: getMsg("NUMQUIRED", lang), minlength: getMsg("NUMQUIRED", lang)} });

        $("a.preview").fancybox(fboptions);
    }
});

function validate(page) {
    var options;
    switch (page){
        case "INDEX":
            options = { type: "POST", url: "/asp/2010stamppoll/validate.asp", success: disableIndexVote };
            break;
        case "POLL":
            options = { type: "POST", url: "/asp/2010stamppoll/validate.asp", success: disablePollVote };
            break;
        default:
            options = "";
            break;
    }
    $.ajax(options);
}

function disableIndexVote(responseText, statusText, xhr, $form) {
    if (responseText == "EXPIRED") {
        $("#StampPoll2010 #eVote p").html('<a href="#" style="color: red;">' + getMsg("EXPIRED", lang) + '</a>');
        $("#StampPoll2010 #eVote em").hide();
    }
}

function disablePollVote(responseText, statusText, xhr, $form) {
    if (responseText == "EXPIRED") {
        $("#PollForm input").attr("disabled", "disabled");
        alert(getMsg("EXPIRED", lang));
    }
}

function getPageLang() {
    var lang = $("meta[http-equiv=Content-Language]").attr("content");
    if (!lang) {
        var lang = $("meta[httpEquiv=Content-Language]").attr("content");
    }
    switch (lang) {
        case "zh-tw":
            return "chi";
            break;
        case "zh-cn":
            return "chis";
            break;
        case "en-gb":
            return "eng";
            break;
        case "pt":
            return "por";
            break;
        default:
            return "eng";
            break;
    }
}

function getMsg(desc, lang) {
    switch (desc) {
        case "SOUVENIRQUIRED":
            switch (lang) {
                case "chi":
                    return "每人限選小型張最多三項";
                    break;
                case "chis":
                    return "每人限选小型张最多三项";
                    break;
                case "por":
                    return "Por favor seleccione um máximo de três blocos";
                    break;
                case "eng":
                    return "Please select a maximum of three souvenir sheets";
                    break;
            }
            break;
        case "STAMPQUIRED":
            switch (lang) {
                case "chi":
                    return "每人限選套票最多三項";
                    break;
                case "chis":
                    return "每人限选套票最多三项";
                    break;
                case "por":
                    return "Por favor seleccione um máximo de três selos";
                    break;
                case "eng":
                    return "Please select a maximum of three stamps";
                    break;
            }
            break;
        case "NAMEQUIRED":
            switch (lang) {
                case "chi":
                    return "請填寫姓名";
                    break;
                case "chis":
                    return "请填写姓名";
                    break;
                case "por":
                    return "Preencha o seu nome";
                    break;
                case "eng":
                    return "Please fill in the name";
                    break;
            }
            break;
        case "IDNUMQUIRED":
            switch (lang) {
                case "chi":
                    return "請填寫身份證號碼";
                    break;
                case "chis":
                    return "请填写身份证号码";
                    break;
                case "por":
                    return "Preencha o número do BIR";
                    break;
                case "eng":
                    return "Please fill in the ID card number";
                    break;
            }
            break;
        case "TELQUIRED":
            switch (lang) {
                case "chi":
                    return "請填寫聯絡電話";
                    break;
                case "chis":
                    return "请填写联络电话";
                    break;
                case "por":
                    return "Preencha o seu número de telefone";
                    break;
                case "eng":
                    return "Please fill in the telephone number";
                    break;
            }
            break;
        case "EMAILQUIRED":
            switch (lang) {
                case "chi":
                    return "請填寫電郵";
                    break;
                case "chis":
                    return "请填写电邮";
                    break;
                case "por":
                    return "Preencha o endereço de e-mail";
                    break;
                case "eng":
                    return "Please fill in the email";
                    break;
            }
            break;
        case "NUMQUIRED":
            switch (lang) {
                case "chi":
                    return "請填寫正確的號碼";
                    break;
                case "chis":
                    return "请填写正确的号码";
                    break;
                case "por":
                    return "Preencha o número correcto";
                    break;
                case "eng":
                    return "Please fill in the correct number";
                    break;
            }
            break;
        case "EMAILVALID":
            switch (lang) {
                case "chi":
                    return "請填寫正確的電郵";
                    break;
                case "chis":
                    return "请填写正确的电邮";
                    break;
                case "por":
                    return "Preencha um endereço de e-mail correcto";
                    break;
                case "eng":
                    return "Please fill in the correct email";
                    break;
            }
            break;
        case "EXPIRED":
            switch (lang) {
                case "chi":
                    return "投票已經截止";
                    break;
                case "chis":
                    return "投票已经截止";
                    break;
                case "por":
                    return "A votação já foi terminada";
                    break;
                case "eng":
                    return "The voting has been terminated";
                    break;
            }
            break;
    }
}